package com.hjj.controller;

import com.hjj.beans.Subject;
import com.hjj.dto.ResultDTO;
import com.hjj.services.SubjectServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/subject")
public class SubjectController {
    @Autowired
    SubjectServices subjectServices;

    @RequestMapping(value = "getAll.action", method = RequestMethod.GET)
    public ResultDTO getAll(){
        ArrayList<Subject> all = subjectServices.getAll();

        // 封装数据传输对象DTO
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        resultDTO.setReason("查询成功");
        resultDTO.setData(all);

        return resultDTO;
    }

    @RequestMapping(value = "editBefore.action", method = RequestMethod.GET)
    public ResultDTO editBefore(int id){
        Subject subject = subjectServices.get(id);

        // 封装数据传输对象DTO
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        resultDTO.setReason("查询成功");
        resultDTO.setData(subject);

        return resultDTO;
    }

    @RequestMapping(value = "add.action", method = RequestMethod.POST)
    public ResultDTO add(Subject subject){
        boolean add = subjectServices.add(subject);

        // 封装数据传输对象DTO
        ResultDTO resultDTO = new ResultDTO();

        if (add){
            resultDTO.setResult(ResultDTO.SUCCESS);
            resultDTO.setReason("添加成功");
            resultDTO.setData(add);
        }else {
            resultDTO.setResult(ResultDTO.ERROR);
            resultDTO.setError("添加失败");
        }


        return resultDTO;
    }

    @RequestMapping(value = "edit.action", method = RequestMethod.POST)
    public ResultDTO edit(Subject subject){
        boolean edit = subjectServices.edit(subject);

        // 封装数据传输对象DTO
        ResultDTO resultDTO = new ResultDTO();

        if (edit){
            resultDTO.setResult(ResultDTO.SUCCESS);
            resultDTO.setReason("修改成功");
            resultDTO.setData(edit);
        }else {
            resultDTO.setResult(ResultDTO.ERROR);
            resultDTO.setError("修改失败");
        }


        return resultDTO;
    }


    @RequestMapping(value = "del.action", method = RequestMethod.GET)
    public ResultDTO del(int id){
        boolean del = subjectServices.del(id);

        // 封装数据传输对象DTO
        ResultDTO resultDTO = new ResultDTO();

        if (del){
            resultDTO.setResult(ResultDTO.SUCCESS);
            resultDTO.setReason("删除成功");
            resultDTO.setData(del);
        }else {
            resultDTO.setResult(ResultDTO.ERROR);
            resultDTO.setError("删除失败");
        }


        return resultDTO;
    }

}
